{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性代数\n",
    "> 线性代数（如矩阵乘法、矩阵分解、行列式以及其他方阵数学等）是任何数组库的重要组成部分。不像某些语言（如MATLAB），通过*对两个二维数组相乘得到的是一个元素级的积，而不是一个矩阵点积。因此，NumPy提供了一个用于矩阵乘法的dot函数（既是一个数组方法也是numpy命名空间中的一个函数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.linalg import inv, qr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "y = np.array([[6., 23.], [-1, 7], [8, 9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3.],\n",
       "       [4., 5., 6.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6., 23.],\n",
       "       [-1.,  7.],\n",
       "       [ 8.,  9.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 28.,  64.],\n",
       "       [ 67., 181.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dot(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6., 15.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一个二维数组跟一个大小合适的一维数组的矩阵点积运算之后 将会得到一个一维数组\n",
    "np.dot(x, np.ones(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.89335586,  0.28637004, -0.11177101, -0.22033902, -0.7314713 ],\n",
       "       [ 0.28637004,  0.8890795 ,  2.15606236,  0.18881362,  1.72991933],\n",
       "       [-0.11177101,  2.15606236,  8.347004  ,  0.60151026,  6.67240435],\n",
       "       [-0.22033902,  0.18881362,  0.60151026,  0.3934082 ,  0.80541826],\n",
       "       [-0.7314713 ,  1.72991933,  6.67240435,  0.80541826,  6.80297727]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.random.randn(5, 5)\n",
    "mat = X.T.dot(X)\n",
    "inv(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.00000000e+00, -1.30744270e-16,  7.32439133e-17,\n",
       "        -2.34874777e-16, -8.80956865e-16],\n",
       "       [-9.18204225e-17,  1.00000000e+00,  5.91386645e-16,\n",
       "        -8.21520815e-17,  1.69147793e-15],\n",
       "       [-4.65615955e-17,  1.03813165e-17,  1.00000000e+00,\n",
       "         2.91700664e-17,  1.52696067e-16],\n",
       "       [ 1.84893831e-16, -6.48885126e-17, -2.73164078e-16,\n",
       "         1.00000000e+00, -7.56056566e-16],\n",
       "       [ 1.47773970e-16, -1.38703622e-16,  8.83584278e-17,\n",
       "        -3.92676496e-17,  1.00000000e+00]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat.dot(inv(mat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "q, r = qr(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.66627193, -0.56952463, -0.27674486,  0.38665523, -0.07505612],\n",
       "       [ 0.66160141, -0.69750307, -0.21036289, -0.00344651,  0.17750667],\n",
       "       [ 0.02968938,  0.36240936, -0.53602765,  0.33421723,  0.68465407],\n",
       "       [-0.24468471, -0.06384193, -0.47437933, -0.83915901,  0.08264381],\n",
       "       [-0.24002754, -0.23176459,  0.60563879, -0.18600226,  0.69805213]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-5.56834158,  7.58983688, -0.22545636, -2.95939637, -1.99251398],\n",
       "       [ 0.        , -2.41055892,  0.97986115,  0.36803587, -0.42571724],\n",
       "       [ 0.        ,  0.        , -0.87632776, -2.38623722,  1.23104497],\n",
       "       [ 0.        ,  0.        ,  0.        , -2.74158473,  0.29724047],\n",
       "       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.1026098 ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的numpy.linalg函数\n",
    "|函数|说明|\n",
    "|:-|:-|\n",
    "|diag|以一维数组的形式返回方阵的对角线(或非对角线)元素，或将一维数组转换为方阵(非对角线元素为0)|\n",
    "|dot|矩阵乘法|\n",
    "|trace|计算对角线元素的和|\n",
    "|det|计算矩阵行列式|\n",
    "|eig|计算方阵的本值和本征向量|\n",
    "|inv|计算方阵的逆|\n",
    "|pinv|计算方阵的Moore-Penrose伪逆|\n",
    "|qr|计算QR分解|\n",
    "|svd|计算奇异值分解(SVD)|\n",
    "|solve|解线性方程组Ax=b，其中A为一个方阵|\n",
    "|lstsq|计算Ax=b的最小二乘解|"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5096c42f2c30fb43a5f4497165333f9bd77160b707603bacb4d0760d03b54e0a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
